package com.itic.system.market.product.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.applications.sys.user.persistence.model.SysUser;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.system.market.product.persistence.dao.ProductDao;
import com.itic.system.market.product.persistence.model.Product;
import com.itic.system.market.product.persistence.model.ProductExample;
import com.itic.system.market.productcollect.persistence.dao.ProductCollectDao;
import com.itic.system.market.productcollect.persistence.model.ProductCollect;
import com.itic.system.market.productcollect.persistence.model.ProductCollectExample;
import com.itic.system.market.productitem.persistence.dao.ProductItemDao;
import com.itic.system.market.productitem.persistence.model.ProductItem;
import com.itic.system.market.productitem.persistence.model.ProductItemExample;
import com.itic.system.market.productitem.persistence.model.ProductItemExample.Criteria;
import com.itic.system.order.orderproduct.persistence.dao.OrderProductDao;
import com.itic.system.order.orderproductitem.persistence.dao.OrderProductItemDao;
import com.itic.system.order.orderproductitem.persistence.model.OrderProductItem;
import com.itic.system.order.orderproductitem.persistence.model.OrderProductItemExample;

@Service
@Transactional
public class ProductService extends CrudService<ProductDao, Product, ProductExample> {
	@Autowired
	private ProductDao productDao;
	@Autowired
	private ProductCollectDao productCollectDao;
	@Autowired
	private ProductItemDao productItemDao;
	@Autowired
	private OrderProductItemDao orderProductItemDao;

	/**
	 * 根据商品获得商品视频项
	 * 
	 * @param product
	 * @return
	 */
	public List<ProductItem> getProductItems(Product product) {
		ProductItemExample example = new ProductItemExample();
		example.setOrderByClause("REORDER ASC");
		Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		criteria.andProductIdEqualTo(product.getId());
		return productItemDao.selectByExample(example);
	}

	/**
	 * 判断用户是否购买了当前商品项
	 * 
	 * @param user
	 * @param productItem
	 * @return
	 */
	public boolean isBuyProductItem(SysUser user, ProductItem productItem) {
		Boolean result = false;
		OrderProductItemExample example = new OrderProductItemExample();
		com.itic.system.order.orderproductitem.persistence.model.OrderProductItemExample.Criteria criteria = example
				.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		criteria.andUserIdEqualTo(user.getId());
		criteria.andProductIdEqualTo(productItem.getProductId());
		criteria.andProductItemIdEqualTo(productItem.getId());
		criteria.andNoteEqualTo(SUCCESS);
		List<OrderProductItem> list = orderProductItemDao.selectByExample(example);
		if (!list.isEmpty()) {
			result = true;
		}
		return result;
	}

	/**
	 * 用户是否收藏这个商品
	 * @param user
	 * @param product
	 * @return
	 */
	public String isCollectByUser(SysUser user, Product product) {
		String result = "noCollected";
		ProductCollectExample example = new ProductCollectExample();
		com.itic.system.market.productcollect.persistence.model.ProductCollectExample.Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		criteria.andProductIdEqualTo(product.getId());
		criteria.andUserIdEqualTo(user.getId());
		List<ProductCollect> list = productCollectDao.selectByExample(example);
		if( !list.isEmpty() ){
			result = "collected";
		}
		return result;
	}

	/**
	 * 清加播放数量
	 * @param product
	 */
	public String addPlayCounts(Product product) {
		product.setPlayCounts(product.getPlayCounts()==null?0:product.getPlayCounts()+1);
		productDao.updateByPrimaryKeySelective(product);
		return SUCCESS;
	}
	
	/**
	 * 清加播放数量
	 * @param product
	 */
	public String addPlayCounts(ProductItem productItem) {
		Product product = productDao.selectByPrimaryKey(productItem.getProductId());
		product.setPlayCounts(product.getPlayCounts()+1);
		productDao.updateByPrimaryKeySelective(product);
		return SUCCESS;
	}

	/**
	 * 添加商品
	 * @param temp
	 * @param items
	 * @return
	 */
	public String addProduct(Product temp, String items) {
		this.initDefalutFields(temp);
		if( StringHelper.isNotBlank(items) ){
			String[] split = items.split(",");
			String mainImage = "";
			for( int i=0; i<split.length; i++ ){
				String item = split[i];
			    String[] split2 = item.split(";");
			    ProductItem productItem = new ProductItem();
			    productItem.setIsVipVideo(split2[0]);
			    productItem.setFee(Double.parseDouble(split2[1]));
			    productItem.setVideoUrl(split2[2]);
			    productItem.setImage(split2[3]);
			    productItem.setSummary(split2[4]);
			    productItem.setProductId(temp.getId());
			    //String url = "http://develop.jhjvip.cn:8081/ddt/mobile/ffmpeg/getVideoCoverPic?videoUrl="+productItem.getVideoUrl();
        		//String httpGet = HttpRequestUtils.httpGet(url);
        		if( i==0 ){
        			mainImage = productItem.getImage();
        		}
			    this.initDefalutFields(productItem);
			    productItemDao.insert(productItem);
			}
			temp.setProductImage(mainImage);
			temp.setPlayCounts(0);
			temp.setUploadTime(new Date());
			productDao.insert(temp);
		}
		return SUCCESS;
	}

	/**
	 * 编辑商品信息
	 * @param temp
	 * @param items
	 * @return
	 */
	public String editProduct(Product temp, String items) {
		if( StringHelper.isNotBlank(items) ){
			String[] split = items.split(",");
			String mainImage = "";
			//删除之前的
			ProductItemExample example = new ProductItemExample();
			Criteria criteria = example.createCriteria();
			criteria.andIsEffectiveEqualTo(DBConst.TRUE);
			criteria.andProductIdEqualTo(temp.getId());
			ProductItem delItem = new ProductItem();
			delItem.setIsEffective(DBConst.FALSE);
			productItemDao.updateByExampleSelective(delItem, example);
			
			for( int i=0; i<split.length; i++ ){
				String item = split[i];
			    String[] split2 = item.split(";");
			    ProductItem productItem = null;
			    productItem = new ProductItem();
			    productItem.setIsVipVideo(split2[1]);
			    productItem.setFee(Double.parseDouble(split2[2]));
			    productItem.setVideoUrl(split2[3]);
			    productItem.setImage(split2[4]);
			    productItem.setSummary(split2[5]);
			    productItem.setProductId(temp.getId());
			    /*String url = "http://develop.jhjvip.cn:8081/ddt/mobile/ffmpeg/getVideoCoverPic?videoUrl="+productItem.getVideoUrl();
        		String httpGet = HttpRequestUtils.httpGet(url);
        		productItem.setImage(httpGet);*/
			    this.initDefalutFields(productItem);
			    productItemDao.insert(productItem);
			    if( i==0 ){
        			mainImage = productItem.getImage();
        		}
			    
			}
			this.initDefalutFields(temp);
			temp.setProductImage(mainImage);
			productDao.updateByPrimaryKey(temp);
		}
		return SUCCESS;
	}
}